package com.chx.three.mama.activities;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Shader;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.chx.three.mama.R;
import com.chx.three.mama.adapter.BeautyDetailAdapter;
import com.chx.three.mama.model.BeautyDetail;
import com.chx.three.mama.view.SildingFinishLayout;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.LinkedList;
import java.util.List;

public class BeautyItemDetailActivity extends Activity implements SildingFinishLayout.OnSildingFinishListener, View.OnClickListener {

    private RequestQueue requestQueue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_beauty_item_detail);

        final int[] levelsRId={R.drawable.myinfo_level_lv1,R.drawable.myinfo_level_lv2,R.drawable.myinfo_level_lv3,
                R.drawable.myinfo_level_lv4,R.drawable.myinfo_level_lv5,R.drawable.myinfo_level_lv6,
                R.drawable.myinfo_level_lv7,R.drawable.myinfo_level_lv8,R.drawable.myinfo_level_lv9,
                R.drawable.myinfo_level_lv10,R.drawable.myinfo_level_lv11,R.drawable.myinfo_level_lv12,
                R.drawable.myinfo_level_lv13,R.drawable.myinfo_level_lv14,R.drawable.myinfo_level_lv15,
                R.drawable.myinfo_level_lv16,R.drawable.myinfo_level_lv17,R.drawable.myinfo_level_lv18,
                R.drawable.myinfo_level_lv19,R.drawable.myinfo_level_lv20};

        SildingFinishLayout finishLayout = (SildingFinishLayout) findViewById(R.id.beauty_item_slidingFinish);
        finishLayout.setOnSildingFinishListener(this);

        ImageButton back = (ImageButton) findViewById(R.id.beauty_item_detail_back);
        back.setOnClickListener(this);

        final TextView titleTxt = (TextView) findViewById(R.id.item_home_beauty_title);
        final TextView reNumTxt = (TextView) findViewById(R.id.item_home_beauty_re_num);
        final ImageView face_img = (ImageView) findViewById(R.id.beauty_item_face_img);
        final TextView nickName = (TextView) findViewById(R.id.beauty_item_nickname);
        final ImageView level = (ImageView) findViewById(R.id.beauty_item_level);
        final TextView itemState = (TextView) findViewById(R.id.beauty_item_state);
        final TextView itemCity = (TextView) findViewById(R.id.beauty_item_city);
        final TextView itemTime = (TextView) findViewById(R.id.beauty_item_time);
        ListView contentListView = (ListView) findViewById(R.id.beauty_item_content_listView);
        final LinkedList<BeautyDetail> beautyDetails = new LinkedList<>();
        final BeautyDetailAdapter adapter = new BeautyDetailAdapter(beautyDetails, this);
        contentListView.setAdapter(adapter);

        Intent intent = getIntent();
        String beautyId = intent.getStringExtra("beautyId");

        requestQueue= Volley.newRequestQueue(this);
        String path="http://www.qubaobei.com/ios/api/adr_view.php?mobile=61&id="+beautyId+"&limit=20&order=1";
        JsonObjectRequest request=new JsonObjectRequest(path,new JSONObject(),
                new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject object) {
                try {
                    JSONObject data = object.getJSONObject("data");
                    titleTxt.setText(data.getString("title"));
                    reNumTxt.setText(data.getString("re_num"));
                    ImageRequest imageRequest=new ImageRequest(data.getString("avatar"),
                            new Response.Listener<Bitmap>() {
                                @Override
                                public void onResponse(Bitmap bitmap) {
                                    int width=bitmap.getWidth();
                                    Bitmap dst = Bitmap.createBitmap(width, width, Bitmap.Config.ARGB_8888);
                                    Paint paint = new Paint();
                                    paint.setShader(new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
                                    Canvas canvas = new Canvas(dst);
                                    canvas.drawCircle(width / 2, width / 2, width / 2, paint);
                                    face_img.setImageBitmap(dst);
                                }
                            },0,0, Bitmap.Config.ARGB_8888,
                            new Response.ErrorListener() {
                                @Override
                                public void onErrorResponse(VolleyError volleyError) {

                                }
                            });
                    requestQueue.add(imageRequest);
                    nickName.setText(data.getString("nickname"));
                    int levels = data.getInt("level")+1;
                    level.setImageResource(levelsRId[levels]);
                    itemState.setText(data.getString("age_str"));
                    itemCity.setText(data.getString("city"));
                    itemTime.setText(data.getString("show_date"));
                    JSONArray content = data.getJSONArray("content");
                    List<BeautyDetail> beautys = JSON.parseArray(content.toString(), BeautyDetail.class);
                    beautyDetails.addAll(beautys);
                    adapter.notifyDataSetChanged();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        },
                new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        });
        requestQueue.add(request);

    }


    @Override
    public void onSildingFinish() {
        super.finish();
    }

    @Override
    public void finish() {
        super.finish();
        this.overridePendingTransition(0,R.anim.push_right_out);
    }

    @Override
    public void onBackPressed() {
        finish();
    }

    @Override
    public void onClick(View v) {
        finish();
    }
}
